/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.serverpackets;

import com.aionengine.gameserver.model.challenge.ChallengeQuest;
import com.aionengine.gameserver.model.challenge.ChallengeTask;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.templates.challenge.ChallengeType;
import com.aionengine.gameserver.network.aion.AionConnection;
import com.aionengine.gameserver.network.aion.AionServerPacket;

import java.util.List;

/**
 * @author ViAl
 */
public class SM_CHALLENGE_LIST extends AionServerPacket {

    int action;
    int ownerId;
    ChallengeType ownerType;
    List<ChallengeTask> tasks;
    ChallengeTask task;

    public SM_CHALLENGE_LIST(int action, int ownerId, ChallengeType ownerType, List<ChallengeTask> tasks) {
        this.action = action;
        this.ownerId = ownerId;
        this.ownerType = ownerType;
        this.tasks = tasks;
    }

    public SM_CHALLENGE_LIST(int action, int ownerId, ChallengeType ownerType, ChallengeTask task) {
        this.action = action;
        this.ownerId = ownerId;
        this.ownerType = ownerType;
        this.task = task;
    }

    @Override
    protected void writeImpl(AionConnection con) {
        Player player = con.getActivePlayer();
        writeC(action);
        writeD(ownerId); // legionId or townId
        writeC(ownerType.getId()); // 1 for legion, 2 for town
        writeD(player.getObjectId());
        switch (action) {
            case 2:  //send challenge tasks list
                writeD((int) (System.currentTimeMillis() / 1000));
                writeH(tasks.size());
                for (ChallengeTask task : tasks) {
                    writeD(32); //unk
                    writeD(task.getTaskId());
                    writeC(1); //unk
                    writeC(21); //unk
                    writeC(0); //unk
                    writeD((int) (task.getCompleteTime().getTime() / 1000));
                }
                break;
            case 7:  //send individual challenge task info
                writeD(32); //unk
                writeD(task.getTaskId());
                writeH(task.getQuestsCount());
                for (ChallengeQuest quest : task.getQuests().values()) {
                    writeD(quest.getQuestId());
                    writeH(quest.getMaxRepeats());
                    writeD(quest.getScorePerQuest());
                    writeH(quest.getCompleteCount()); //unk
                }
                break;
        }
    }

}
